package com.ruoyi.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.LWaybilldetail;
import com.ruoyi.system.mapper.LWaybillMapper;
import com.ruoyi.system.domain.LWaybill;
import com.ruoyi.system.service.ILWaybillService;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;
import static com.ruoyi.system.utils.Code.getCode;
import static com.ruoyi.system.utils.Code.getNowDate;

/**
 * 清运单主Service业务层处理
 *
 * @author zyb
 * @date 2022-01-11
 */
@Service
public class LWaybillServiceImpl extends ServiceImpl<LWaybillMapper, LWaybill> implements ILWaybillService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询清运单主
     *
     * @param billid 清运单主主键
     * @return 清运单主
     */
    @Override
    public LWaybill selectLWaybillByBillid(Long billid) {
        return baseMapper.selectLWaybillByBillid(billid);
    }

    /**
     * 查询清运单主列表
     *
     * @param lWaybill 清运单主
     * @return 清运单主
     */
    @Override
    public List<LWaybill> selectLWaybillList(LWaybill lWaybill) {
        return baseMapper.selectLWaybillList(lWaybill);
    }

    /**
     * 新增清运单主
     *
     * @param lWaybill 清运单主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLWaybill(LWaybill lWaybill) {
        if (!StringUtils.isNotBlank(lWaybill.getBillcode())) {
            lWaybill.setBillcode(String.valueOf(getBillCode()));
        }
        checkBillCode(lWaybill);
        String str = getNowDate();
        int waybill = redisCache.getCacheObject("waybill" + str) == null ? 0 : redisCache.getCacheObject("waybill" + str);
        lWaybill.setCreateTime(DateUtils.getNowDate());
        int rows = baseMapper.insertLWaybill(lWaybill);
        waybill++;
        redisCache.setCacheObject("waybill" + str, waybill, 1, TimeUnit.DAYS);
        insertLWaybilldetail(lWaybill);
        return rows;
    }

    //验证订单编号是否重复
    public void checkBillCode(LWaybill waybill) {
        String Billcode = waybill.getBillcode();
        LWaybill param = new LWaybill();
        param.setBillcode(Billcode);
        List<LWaybill> waybills = selectLWaybillList(param);
        if (waybills.size() > 0) {
            String str = getNowDate();
            String type = sysConfigService.selectConfigByKey("way.bill.code.type");
            String billCode = type + "-" + str + "%";
            int num = baseMapper.selectCountByBillCode(billCode);
            redisCache.setCacheObject("waybill" + str, num, 1, TimeUnit.DAYS);
            waybill.setBillcode(String.valueOf(getBillCode()));
            checkBillCode(waybill, num);
        }


    }

    public void checkBillCode(LWaybill waybill, int num) {
        String Billcode = waybill.getBillcode();
        LWaybill param = new LWaybill();
        param.setBillcode(Billcode);
        List<LWaybill> waybills = selectLWaybillList(param);
        if (waybills.size() > 0) {
            String str = getNowDate();
            num++;
            redisCache.setCacheObject("waybill" + str, num, 1, TimeUnit.DAYS);
            waybill.setBillcode(String.valueOf(getBillCode()));
            checkBillCode(waybill, num);
        }
    }

    /**
     * 修改清运单主
     *
     * @param lWaybill 清运单主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLWaybill(LWaybill lWaybill) {
        lWaybill.setUpdateTime(DateUtils.getNowDate());
        baseMapper.deleteLWaybilldetailByBillid(lWaybill.getBillid());
        insertLWaybilldetail(lWaybill);
        return baseMapper.updateLWaybill(lWaybill);
    }

    /**
     * 批量删除清运单主
     *
     * @param billids 需要删除的清运单主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLWaybillByBillids(Long[] billids) {
        baseMapper.deleteLWaybilldetailByBillids(billids);
        return baseMapper.deleteLWaybillByBillids(billids);
    }

    /**
     * 删除清运单主信息
     *
     * @param billid 清运单主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLWaybillByBillid(Long billid) {
        baseMapper.deleteLWaybilldetailByBillid(billid);
        return baseMapper.deleteLWaybillByBillid(billid);
    }

    @Override
    public Object getBillCode() {
        String str = getNowDate();
        String type = sysConfigService.selectConfigByKey("way.bill.code.type");
        String billCode = type + "-" + str + "-";
        int waybill = redisCache.getCacheObject("waybill" + str) == null ? 0 : redisCache.getCacheObject("waybill" + str);
        waybill++;
        billCode = getCode(billCode, waybill);
        return billCode;
    }

    /**
     * 新增清运单明细信息
     *
     * @param lWaybill 清运单主对象
     */
    public void insertLWaybilldetail(LWaybill lWaybill) {
        List<LWaybilldetail> lWaybilldetailList = lWaybill.getLWaybilldetailList();
        Long billid = lWaybill.getBillid();
        if (StringUtils.isNotNull(lWaybilldetailList)) {
            List<LWaybilldetail> list = new ArrayList<LWaybilldetail>();
            for (LWaybilldetail lWaybilldetail : lWaybilldetailList) {
                lWaybilldetail.setCreateBy(String.valueOf(getUserId()));
                lWaybilldetail.setCreateTime(DateUtils.getNowDate());
                lWaybilldetail.setBillid(billid);
                list.add(lWaybilldetail);
            }
            if (list.size() > 0) {
                baseMapper.batchLWaybilldetail(list);
            }
        }
    }
}
